﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Loja_Virtual_Livraria.App_Code.Model;
using Loja_Virtual_Livraria.App_Code.BLL;

namespace Loja_Virtual_Livraria.App_Code.SQLServerDAL
{
    public class JuridicaDAL
    {
        public static void insertJuridica(Juridica myJuridica)
        {
            SqlConnection myConnection = null;
            try
            {
                myConnection = SQLServerConnection.getConnection();
                SqlCommand myCommand = new SqlCommand("INSERT INTO Juridica (pessoaID, razaoSocial, cnpj, " 
                + "inscEstadual, inscMunicipal, dataFundacao) "
                + "VALUES (@pessoaID, @razaoSocial, @cnpj, @inscEstadual, @inscMunicipal, @dataFundacao)", myConnection);

                SqlParameter paramPessoaId = new SqlParameter("pessoaID", SqlDbType.Int);
                paramPessoaId.Direction = ParameterDirection.Input;
                paramPessoaId.Value = myJuridica.getPessoaID();

                SqlParameter paramRazaoSocial = new SqlParameter("razaoSocial", SqlDbType.VarChar);
                paramRazaoSocial.Direction = ParameterDirection.Input;
                paramRazaoSocial.Value = myJuridica.getRazaoSocial();

                SqlParameter paramCnpj = new SqlParameter("cnpj", SqlDbType.VarChar);
                paramCnpj.Direction = ParameterDirection.Input;
                paramCnpj.Value = myJuridica.getCnpj();

                SqlParameter paramInscEstadual = new SqlParameter("inscEstadual", SqlDbType.VarChar);
                paramInscEstadual.Direction = ParameterDirection.Input;
                paramInscEstadual.Value = myJuridica.getInscEstadual();

                SqlParameter paramInscMunicipal = new SqlParameter("inscMunicipal", SqlDbType.VarChar);
                paramInscMunicipal.Direction = ParameterDirection.Input;
                paramInscMunicipal.Value = myJuridica.getInscMunicipal();

                SqlParameter paramDataFundacao = new SqlParameter("dataFundacao", SqlDbType.DateTime);
                paramDataFundacao.Direction = ParameterDirection.Input;
                paramDataFundacao.Value = myJuridica.getDataFundacao();

                myCommand.Parameters.Add(paramPessoaId);
                myCommand.Parameters.Add(paramRazaoSocial);
                myCommand.Parameters.Add(paramCnpj);
                myCommand.Parameters.Add(paramInscEstadual);
                myCommand.Parameters.Add(paramInscMunicipal);
                myCommand.Parameters.Add(paramDataFundacao);

                myConnection.Open();
                myCommand.ExecuteNonQuery();
            }
            finally
            {
                if (myConnection != null)
                    myConnection.Close();
            }
        }

        public static void updateJuridica(Juridica myJuridica)
        {
            SqlConnection myConnection = null;
            try
            {
                myConnection = SQLServerConnection.getConnection();
                SqlCommand myCommand = new SqlCommand("UPDATE Juridica SET pessoa = @pessoaID, razaoSocial = @razaoSocial, "+
                " inscEstadual = @inscEstadual, inscMunicipal = @inscMunicipal, dataFundacao = @dataFundacao)", myConnection);

                SqlParameter paramPessoaId = new SqlParameter("pessoaID", SqlDbType.Int);
                paramPessoaId.Direction = ParameterDirection.Input;
                paramPessoaId.Value = myJuridica.getPessoaID();

                SqlParameter paramRazaoSocial = new SqlParameter("razaoSocial", SqlDbType.VarChar);
                paramRazaoSocial.Direction = ParameterDirection.Input;
                paramRazaoSocial.Value = myJuridica.getRazaoSocial();

                SqlParameter paramCnpj = new SqlParameter("cnpj", SqlDbType.VarChar);
                paramCnpj.Direction = ParameterDirection.Input;
                paramCnpj.Value = myJuridica.getCnpj();

                SqlParameter paramInscEstadual = new SqlParameter("inscEstadual", SqlDbType.VarChar);
                paramInscEstadual.Direction = ParameterDirection.Input;
                paramInscEstadual.Value = myJuridica.getInscEstadual();

                SqlParameter paramInscMunicipal = new SqlParameter("inscMunicipal", SqlDbType.VarChar);
                paramInscMunicipal.Direction = ParameterDirection.Input;
                paramInscMunicipal.Value = myJuridica.getInscMunicipal();

                SqlParameter paramDataFundacao = new SqlParameter("dataFundacao", SqlDbType.DateTime);
                paramDataFundacao.Direction = ParameterDirection.Input;
                paramDataFundacao.Value = myJuridica.getInscMunicipal();

                myCommand.Parameters.Add(paramPessoaId);
                myCommand.Parameters.Add(paramRazaoSocial);
                myCommand.Parameters.Add(paramCnpj);
                myCommand.Parameters.Add(paramInscEstadual);
                myCommand.Parameters.Add(paramInscMunicipal);
                myCommand.Parameters.Add(paramDataFundacao);

                myConnection.Open();
                myCommand.ExecuteNonQuery();
            }
            finally
            {
                if (myConnection != null)
                    myConnection.Close();
            }
        }
    }
}